package com.ss.whisper2.controller;

import com.ss.whisper2.pojo.Result;
import com.ss.whisper2.pojo.State;
import com.ss.whisper2.pojo.entity.course.Course;
import com.ss.whisper2.pojo.entity.course.SelectCourseMessage;
import com.ss.whisper2.pojo.entity.user.User;
import com.ss.whisper2.rabbitmq.topic.TopicRabbitConfig;
import com.ss.whisper2.rabbitmq.topic.TopicSender;
import com.ss.whisper2.service.CourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class CourseController implements InitializingBean {

	private Logger logger = LoggerFactory.getLogger(getClass());

	private static final String[] FORM={"1-2节","3-4节","5-6节","7-8节","9-10节","11-13节",};

	@Autowired
	CourseService courseService;
	@Autowired
	TopicSender topicSender;

	@RequestMapping("/course-list")
	public String getCourseList(HttpServletRequest request) {
		request.setAttribute("courseList",courseService.getAllCourses());
		return "course-list";
	}

	@GetMapping("/user-course-list")
	public String getUserCourseList(HttpSession session,HttpServletRequest request) {
		String username = ((User)session.getAttribute("user")).getUsername();
		Course[][] userCourseList=new Course[6][7];
		for (Course course : courseService.getSelectedCourses(username)) {
			int day=course.getDay()-1;
			int time=course.getTime()-1;
			userCourseList[day][time]=course;
		}
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				if (userCourseList[i][j] == null) {
					userCourseList[i][j] = new Course();
				}
			}
		}
		request.setAttribute("userCourseList",userCourseList);
		request.setAttribute("form",FORM);
		return "user-course-list";
	}

	@GetMapping("score-list")
	public String getScoreList(HttpSession session,HttpServletRequest request) {
		String username = ((User)session.getAttribute("user")).getUsername();
		request.setAttribute("userCourseList",courseService.getSelectedCourses(username));
		request.setAttribute("userScoreList",courseService.getScoreList(username));
		request.setAttribute("userBehaveScoreList",courseService.getBehaveScoreList(username));
		request.setAttribute("userLastScoreList",courseService.getLastScoreList(username));
		request.setAttribute("userExperimentScoreList",courseService.getExperimentScoreList(username));
		return "score-list";
	}

	@PostMapping("selectCourse")
	@ResponseBody
	public Result<Integer> selectCourse(String username, Integer courseId) {
		if (courseService.getSelectedCourseResult(username, courseId))
			return new Result<>(State.DUPLICATE_SELECT);
		if (courseService.getCourseRest(courseId)<=0)
			return new Result<>(State.NO_REST);
		topicSender.sendSelectCourseMessage(new SelectCourseMessage(username,courseId));
		return new Result<>(State.SUCCESS);
	}

	@GetMapping("getSelectCourseResult")
	@ResponseBody
	public Result<Integer> getSelectCourseResult(String username, Integer courseId) {
		return courseService.getSelectedCourseResult(username,courseId)?new Result<>(State.SUCCESS_SELECT):new Result<>(State.FAIL_SELECT);
	}


	@GetMapping("welcome1")
	public String welcome1(Model model) {
		model.addAttribute("userScoreList","123456");
		return "welcome1";
	}


	@RequestMapping("/deleteCache")
	@CacheEvict(value = "courses",allEntries = true,beforeInvocation = false)
	//删除所有值为courses的缓存
	public String deleteCourseCache() {
		return "redirect:";
	}

	@Override
	public void afterPropertiesSet() throws Exception {

	}
}
